package com.luxk.db.block;

import com.luxk.util.ByteArrayUtil;

public class BlockSpaceHeaderHelper {
	
	/* format
	 * baseOffset + 0, 2byte : freespace
	 * baseOffset + 2, 2byte : freepos
	 */
	private static final int SIZE_FREESPACE = 2;
	private static final int SIZE_FREEPOS = 2;

	/*
	private static final int BITMASK_UPPER = 0xF0;	
	private static final int BITMASK_LOWER = 0x0F;
	
	public static int getExtFreenessCount(byte[] buffer, int baseOffset)
			throws Exception {
		return (int)ByteArrayUtil.readChar(buffer, baseOffset);
	}
	
	public static void setExtFreenessCount(byte[] buffer, int baseOffset,
			int cnt) throws Exception {
		if(cnt > Character.MAX_VALUE)
			throw new Exception(
					"Maximum freeness count exceeded(" + cnt + ")");
		
		if(cnt % 2 != 0)
			throw new Exception(
					"Freeness count(" + cnt + ") must be even");

		ByteArrayUtil.writeChar((char)cnt, buffer, baseOffset);
	}
	
	public static Freeness getFreeness(byte[] buffer, int baseOffset, int i) {
		int offset = baseOffset + OFFSET_FREENESS_ARR;
		offset += (i/2);
		byte b = buffer[offset];
		int val = (i%2 == 0) ? (b & BITMASK_LOWER) : (b & BITMASK_UPPER >> 4);
		return Freeness.getFreeness(val);
	}
	
	public static void setFreeness(byte[] buffer, int baseOffset, int i, 
			Freeness freeness) {
		int offset = baseOffset + OFFSET_FREENESS_ARR;
		offset += (i/2);
		int val = freeness.getValue();
		
		if(i % 2 == 0) {
			// clear and set lower 4bits
			buffer[offset] &= ~BITMASK_LOWER;
			buffer[offset] |= val;
		}
		else {
			// clear and set upper 4bits
			buffer[offset] &= ~BITMASK_UPPER;
			buffer[offset] |= val << 4;
		}
	}
	
	public static int getFreespace(byte[] buffer, int baseOffset)
			throws Exception {
		int offset = baseOffset + OFFSET_FREENESS_ARR + 
			(BlockSpaceHeaderHelper.getExtFreenessCount(buffer, baseOffset)/2);
		return ByteArrayUtil.readChar(buffer, offset);
	}
	
	public static void setFreespace(byte[] buffer, int baseOffset,
			int freespace) throws Exception {
		int offset = baseOffset + OFFSET_FREENESS_ARR + 
			(BlockSpaceHeaderHelper.getExtFreenessCount(buffer, baseOffset)/2);
		ByteArrayUtil.writeChar((char)freespace, buffer, offset);
	}
	
	public static int getFreepos(byte[] buffer, int baseOffset)
			throws Exception {
		int offset = baseOffset + OFFSET_FREENESS_ARR + 
			(BlockSpaceHeaderHelper.getExtFreenessCount(buffer, baseOffset)/2) +
			SIZE_FREESPACE;
		return ByteArrayUtil.readChar(buffer, offset);
	}
	
	public static void setFreepos(byte[] buffer, int baseOffset, int freepos)
			throws Exception {
		int offset = baseOffset + OFFSET_FREENESS_ARR + 
			(BlockSpaceHeaderHelper.getExtFreenessCount(buffer, baseOffset)/2) +
			SIZE_FREESPACE;
		ByteArrayUtil.writeChar((char)freepos, buffer, offset);		
	}

	public static int getSpaceHeaderSize(byte[] buffer, int baseOffset)
			throws Exception {
		return (OFFSET_FREENESS_ARR + 
			(BlockSpaceHeaderHelper.getExtFreenessCount(buffer, baseOffset)/2) +
			SIZE_FREESPACE + SIZE_FREEPOS);
	}
	*/
	
	public static int getFreespace(byte[] buffer, int baseOffset)
			throws Exception {
		return ByteArrayUtil.readChar(buffer, baseOffset);
	}
	
	public static void setFreespace(byte[] buffer, int baseOffset,
			int freespace) throws Exception {
		ByteArrayUtil.writeChar((char)freespace, buffer, baseOffset);
	}
	
	public static int getFreepos(byte[] buffer, int baseOffset)
			throws Exception {
		int offset = baseOffset + SIZE_FREESPACE;
		return ByteArrayUtil.readChar(buffer, offset);
	}
	
	public static void setFreepos(byte[] buffer, int baseOffset, int freepos)
			throws Exception {
		int offset = baseOffset + SIZE_FREESPACE;
		ByteArrayUtil.writeChar((char)freepos, buffer, offset);		
	}

	public static int getSpaceHeaderSize(byte[] buffer, int baseOffset)
			throws Exception {
		return SIZE_FREESPACE + SIZE_FREEPOS;
	}
}
